﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.Security.Cryptography;

namespace SecurityProject
{
    public partial class Registration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected String PasswordHashing()
        {
            string message;
            message = this.CreateUserWizard1.Password;
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

            //MD5 md5 = new MD5CryptoServiceProvider();
            //SHA1 sha1 = new SHA1CryptoServiceProvider();
            SHA256Managed sha256 = new SHA256Managed();
            //SHA384Managed sha384 = new SHA384Managed();
            //SHA512Managed sha512 = new SHA512Managed();

            //md5($salt.md5($pass))
            byte[] messageBytes = encoding.GetBytes(message);
            byte[] hashmessage = sha256.ComputeHash(messageBytes);
            byte[] saltedhash;

            //hashmessage = md5.ComputeHash(messageBytes);
            hashmessage = sha256.ComputeHash(messageBytes);
            // aDD salt.
            string enc = "";
            enc = message + System.Convert.ToBase64String(hashmessage);

            // Get the new byte array after adding the salt.
            saltedhash = sha256.ComputeHash(encoding.GetBytes(enc));

            String hashedPassword = Convert.ToBase64String(saltedhash);
            return hashedPassword;
        }


        protected void CreateUserWizard1_CreatedUser1(object sender, EventArgs e)
        {
            CreateUserWizard wizard = (CreateUserWizard)sender;
            MembershipUser user = Membership.GetUser(wizard.UserName);
            if (user != null)
            {
                try
                {
                    //SmtpClient ss = new SmtpClient("smtp.gmail.com", 465);
                    SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
                    ss.UseDefaultCredentials = false;
                    ss.Credentials = new NetworkCredential("maged.zamzam@gmail.com", "8jSouHrA");
                    ss.EnableSsl = true;
                    //ss.Timeout = 10000;
                    ss.DeliveryMethod = SmtpDeliveryMethod.Network;
                    MailMessage mm = new MailMessage("maged.zamzam@gmail.com", user.Email, "Account Confirmation", "http://localhost:1163/ConfirmRegisteration.aspx?id=" + user.ProviderUserKey.ToString());
                    mm.BodyEncoding = UTF8Encoding.UTF8;
                    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    ss.Send(mm);
                    //SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
                    ////smtpClient.UseDefaultCredentials = true;
                    //smtpClient.Credentials = new NetworkCredential("maged.zamzam@gmail.com", "8jSouHrA");
                    //smtpClient.Send("maged.zamzam@gmail.com", user.Email, "Account Confirmation", "http://host/ConfirmRegisteration.aspx?id=" + user.ProviderUserKey.ToString());
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex.Message);
                }
            }
        }
    }
}